using System;
using System.Collections;
using System.Diagnostics;
using UnityEngine;
using VoxelEngine;

namespace VoxelGame.NPC
{
	public class NPCFactory : MonoBehaviour
	{
		public IEnumerator Create(VoxelEngine.World world, ResourceAsset npcsAsset, float startProgress, float maxProgress, INPCListener listener, Action<float> onProgress = null)
		{
			Stopwatch stopwatch = new Stopwatch();
			stopwatch.Start();
			UnityEngine.Debug.Log("[Voxel] Creating NPCs");
			NPCsData npcsData = null;
			yield return StartCoroutine(npcsAsset.AsyncAsset(delegate(NPCsData asset)
			{
				npcsData = asset;
			}, delegate(float progress)
			{
				onProgress.Fire(startProgress + Mathf.Lerp(0f, maxProgress, progress));
			}));
			for (int i = 0; i < npcsData.NPCs.Length; i++)
			{
				GameObject original = npcsData.NPCs[i];
				GameObject gameObject = UnityEngine.Object.Instantiate(original);
				gameObject.transform.SetParent(base.transform);
				NPCControler component = gameObject.GetComponent<NPCControler>();
				component.RegisterNpcListener(listener);
				component.Init(world);
			}
			stopwatch.Stop();
			UnityEngine.Debug.Log("[Voxel] NPCs created in: " + stopwatch.ElapsedMilliseconds + " ms");
			onProgress.Fire(1f);
		}
	}
}
